package com.efounder.eai.framework;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.caucho.hessian.client.HessianProxyFactory;
import com.core.servlet.AFInvokeMethod;
import com.core.xml.StubObject;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.eai.EAIServer;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.util.AppContext;

/**
 * 
 * @author Pansoft
 *
 */
public class WebInvoke {
	/**
	 * 
	 */
	public WebInvoke() {
		
	}
	/**
	 * 
	 * @param serviceKey
	 * @param po
	 * @return
	 * @throws Exception
	 */
	public JResponseObject SVR(String serviceKey,JParamObject po) throws Exception {
		return SVR(serviceKey,po,null,null,null);
	}
	/**
	 * 
	 * @param serviceKey
	 * @param po
	 * @param d0
	 * @return
	 * @throws Exception
	 */
	public JResponseObject SVR(String serviceKey,JParamObject po,Object d0) throws Exception {
		return SVR(serviceKey,po,d0,null,null);
	}
	/**
	 * 
	 * @param serviceKey
	 * @param po
	 * @param d0
	 * @param co
	 * @return
	 * @throws Exception
	 */
	public JResponseObject SVR(String serviceKey,JParamObject po,Object d0,Object co) throws Exception {
		return SVR(serviceKey,po,d0,co,null);
	}

	/**
	 * 
	 * @param aon
	 * @param amn
	 * @return
	 * @throws Exception
	 */
	public JResponseObject IOM(String aon,String amn) throws Exception {
		return IOM(aon,amn,null,null,null,null);
	}	
	/**
	 * 
	 * @param aon
	 * @param amn
	 * @param po
	 * @return
	 * @throws Exception
	 */
	public JResponseObject IOM(String aon,String amn,JParamObject po) throws Exception {
		return IOM(aon,amn,po,null,null,null);
	}	
	/**
	 * 
	 * @param aon
	 * @param amn
	 * @param po
	 * @param d0
	 * @return
	 * @throws Exception
	 */
	public JResponseObject IOM(String aon,String amn,JParamObject po,Object d0) throws Exception {
		return IOM(aon,amn,po,d0,null,null);
	}
	/**
	 * 
	 * @param aon
	 * @param amn
	 * @param po
	 * @param d0
	 * @param co
	 * @return
	 * @throws Exception
	 */
	public JResponseObject IOM(String aon,String amn,JParamObject po,Object d0,Object co) throws Exception {
		return IOM(aon,amn,po,d0,co,null);
	}
	/**
	 * 
	 * @param aon
	 * @param amn
	 * @param po
	 * @param d0
	 * @param co
	 * @param ao
	 * @return
	 * @throws Exception
	 */
	public JResponseObject IOM(String aon,String amn,JParamObject po,Object d0,Object co,Object ao) throws Exception {
		JResponseObject ro = null;			
		JParamObject jpo = po;
		HessianProxyFactory factory = new HessianProxyFactory();
		try {

			//String url = "http://cloud.pansoft.com:8080/EnterpriseServerMobile/Android";//getServerUrl(aon,po);//"http://192.168.0.185:8088/EnterpriseServer/Android";
			String url=getServerUrl(aon,po);
			factory.setDebug(false);
//			factory.setReadTimeout(55000);
			po.setEnvValue("_client_platform_", "android");
			AFInvokeMethod basic = factory.create(AFInvokeMethod.class, url, this.getClass().getClassLoader());
			factory.setOverloadEnabled(true);
			factory.setHessian2Reply(false);
			ro = (JResponseObject)basic.IOM(aon, amn,po,d0,co,ao);
		} catch (Exception e) {
			e.printStackTrace();
		}
		WebInvoke.checkAuthorization(ro);
		return ro;		
	}
	/**
	 * 
	 * @param serviceKey
	 * @param po
	 * @param d0
	 * @param co
	 * @param ao
	 * @return
	 * @throws Exception
	 */
	public JResponseObject SVR(String serviceKey,JParamObject po,Object d0,Object co,Object ao) throws Exception {
		JResponseObject ro = null;		
		HessianProxyFactory factory = new HessianProxyFactory();
		try {
			//String url = "http://cloud.pansoft.com:8080/EnterpriseServerMobile/Android";//getServerUrl(serviceKey,po);//"http://192.168.0.185:8088/EnterpriseServer/Android";
			String url=getServerUrl(serviceKey,po);
			factory.setDebug(false);
//			factory.setReadTimeout(55000);
			po.setEnvValue("_client_platform_", "android");
			AFInvokeMethod basic = factory.create(AFInvokeMethod.class, url, this.getClass().getClassLoader());
			factory.setOverloadEnabled(true);
			factory.setHessian2Reply(false);
			ro = (JResponseObject)basic.SVR(serviceKey,po,d0,co,ao);
		} catch (Exception e) {
			e.printStackTrace();
		}
		WebInvoke.checkAuthorization(ro);
		return ro;			
	}
	/**
	 * 验证是否授权  fsq
	 * @param ro
	 * @throws Exception 
	 */
	public static void checkAuthorization(JResponseObject ro) throws Exception{
		if(ro == null || ro.getResponseMap() == null)	return;
		//如果没有继承APPContext 抛出异常
		if(AppContext.getInstance() == null) throw new Exception();
		
		String authorization = (String) ro.getResponseMap().get("Authorization");
		String result = "";
		if(authorization != null && authorization.equals("FALSE")){
			result = "FALSE";
			EnvironmentVariable.setProperty("authorization", "FALSE");
//			   Message m = new Message();  
//               m.what = 0x111;
               // 发送消息  
//               Handler handler = new Handler();
//               handler.sendMessage(m);
		}else{
	        result = "TRUE";
			EnvironmentVariable.setProperty("authorization", "TRUE");
		}
				
		//派发验证的广播
		Intent intent = new Intent();
		intent.setAction("com.efounder.action.authorization");
		intent.putExtra("authorization", result);
		AppContext.getInstance().sendBroadcast(intent);	
	}
	/**
	 * 
	 * @param objsvrName
	 * @param paramObject
	 * @return
	 * @throws Exception 
	 */
	public static String getServerUrl(String objsvrName,JParamObject paramObject) throws Exception {
		return getURL(paramObject, objsvrName);
	}
	/**
	 * 
	 * @param PO
	 * @param objectName
	 * @return
	 * @throws Exception 
	 */
	public static String getURL(JParamObject PO, String objectName) throws Exception {
		String uri  = null;
		String defUrl = EAIServer.getDefaultUrl();
		String serverName = getObjectOnServer(PO, objectName);
		//add end
		// 先获取PO中的ServiceURL，如果设置了它，就用，优先级最高
		uri = PO.getServiceURL();
		if ( uri != null && uri.trim().length() != 0 ) {
			return uri;
		}
		// 如果ServerName为空，则使用默认的连接，在后台，ServerName肯定不为空
		if (serverName == null || serverName.trim().length() == 0) {
			return defUrl;
		} else {
		    StubObject serverStub = EAIServer.getEAIServer(serverName);
		    if (serverStub == null) {
		    	return defUrl;
		    }
		    // 返回指定Server的URI
		    uri = EAIServer.getEAIServerURI(serverStub);

		    com.efounder.eai.data.JParamObject po = PO;
		    // 设置相应的数据库设置
		    String dbset = serverStub.getString(EAIServer.SERVER_DBID, null);
		    if (dbset != null && dbset.trim().length() > 0) {
		    	  po.SetValueByEnvName("DataBaseName", dbset);
		    }		
		    dbset = serverStub.getString(EAIServer.SERVER_ZTID, null);
		    if (dbset != null && dbset.trim().length() > 0) {
		    	po.SetValueByEnvName("DBNO", dbset);
		    }
		    return uri;
		}
	}	
	/**
	 * 
	 * @param paramObject
	 * @param objectName
	 * @return
	 */
	public static String getObjectOnServer(JParamObject paramObject, String objectName) {
		String serverID = null;
		// 新PO中设置的EAIServer优先级最高
		serverID = paramObject.getEAIServer(null);
		if (serverID != null && serverID.trim().length() > 0 ) {
			return serverID;
		}
		// 对象名中设置的EAIServer次之
		if (objectName.indexOf(".") != -1) {
			serverID = objectName.substring(0, objectName.indexOf("."));
		} else {
			// Package XML中定义的最低
			serverID = EAIServer.getDALObjectServer(objectName);
		}
		if (serverID != null && serverID.trim().length() > 0 ) {
			return serverID;
		}
		return null;
	}	
}
